package dongshi.daddy.lab;

import java.util.Random;

/*
演示GC日志生成与解读
*/
public class GCLogAnalysis {
    private static Random random = new Random();
    public static void main(String[] args) {
        // 136****b***
//         System.out.println(toStar1("1363635bcde", "(.{3}).{4}(.{1}).{3}"));
        // 中间加星 136****bcde
//         System.out.println(toStar1("1363635bcde", "(.{3}).{4}(.{4})"));
        // 男后一个字符变成星,男前不知道有多少个字符，写(.) 1363635b男*e
//        System.out.println(toStar1("1363635b男de", "(.)(男).{1}(.{1})"));
      // 男后3个字符变成星,男前不知道有多少个字符，写(.),最后不知道有多个字符，写(.) 1363635b男***fdafwefwegweg
//    System.out.println(toStar1("1363635b男dedfdafwefwegweg", "(.)(男).{3}(.)"));
        // 男后2个字符变成，男后3个字符变成星,男前不知道有多少个字符，写(.),最后不知道有多个字符，写(.) 136363**男***fdafwefwegweg
     //   System.out.println(toStar1("1363635b男dedfdafwefwegweg", "(.).{2}(男).{3}(.)"));

        // 开始2个字符变成星，男后3个字符变成星,男前不知道有多少个字符，写(.),最后不知道有多个字符，写(.)

    }


    public static String toStar1(String content, String regString) {
        // return content.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1*****$2");
        // return content.replaceAll("(.{3}).{4}(.{1}).{3}","$1***$2***");
        char leftBracket =  '(';
        char rightBracket =  ')';
        String replaceStr = "";
        boolean foundLeftBracket = false;
        boolean foundRightBracket = false;
        int groupNum = 1;
        for (int i = 0; i < regString.length(); i++) {
            char curChar = regString.charAt(i);
            // 如果是(则直接匹配到最后的)
            if (curChar == '(') {
                // 获取)的位置
                // int rightBracketIndx = regString.lastIndexOf(")", i);
                int rightBracketIndx = regString.indexOf(")", i);
                // 往后走
                // i = rightBracketIndx + 1;
                i = rightBracketIndx;
                replaceStr += "$" + (groupNum++);
            } else { // 不是(的情况
                // {的位置
                int leftBigBracketPos = regString.indexOf("{", i);
                // }的位置
                int rightBigBracketPos = regString.indexOf("}", i);
                int startNum  = Integer.valueOf(regString.substring(leftBigBracketPos + 1, rightBigBracketPos));
                // while (startNum-- >= 0) {
                while (startNum-- > 0) {
                    replaceStr += "*";
                }
                i = rightBigBracketPos;
            }
        }
        return content.replaceAll(regString, replaceStr);
    }


    // return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1*****$2");
    public static String toStar(String content) {
        // return content.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1*****$2");
        return content.replaceAll("(.{3}).{4}(.{1}).{3}","$1***$2***");

    }


    // 生成对象
    // 创建不同的对象模拟业务中真实对象的创建
    private static Object generateGarbage(int max) {
        int randomSize = random.nextInt(max);
        int type = randomSize % 4;
        Object result = null;
        switch (type) {
            case 0:
                result = new int[randomSize];
                break;
            case 1:
                result = new byte[randomSize];
                break;
            case 2:
                result = new double[randomSize];
                break;
            default:
                StringBuilder builder = new StringBuilder();
                String randomString = "randomString-Anything";
                while (builder.length() < randomSize) {
                    builder.append(randomString);
                    builder.append(max);
                    builder.append(randomSize);
                }
                result = builder.toString();
                break;
        }
        return result;
    }
}